package com.xoa.service.property;

import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.fixAssets.EduFixAssetsMapper;
import com.xoa.dao.property.MaintainMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.fixAssets.EduFixAssets;
import com.xoa.model.property.Maintain;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MaintainService {

    @Autowired
    MaintainMapper maintainMapper;
    @Autowired
    private UsersMapper usersMapper;


    public ToJson<Object> insertSelective(Maintain record,String id) {
        ToJson<Object> json = new ToJson<Object> ( 1, "err" );
        try {
            int count = maintainMapper.insertSelective ( record );

            if (count > 0) {
                json.setMsg ( "创建成功" );
                json.setFlag ( 0 );
            }

        } catch (Exception e) {
            json.setMsg ( e.getMessage () );
            L.e ( "MaintainService insertSelective:" + e );
        }
        return json;

    }


    public ToJson<Object> selectByPrimaryKey(Integer id) {
        ToJson<Object> json = new ToJson<Object> ( 1, "err" );
        try {
            Maintain maintain = maintainMapper.selectByPrimaryKey ( id );
            //当前使用者
            String records = maintain.getUser();
            String[] split = records.split ( "," );
            String receive = "";
            if (split != null && split.length > 0) {//判断
                for (String s : split) {
                    String name = usersMapper.getUsernameByUserId ( s );
                    if (name != null && name != "") {
                        receive = receive + name + ",";
                    }

                }
            }
            if (receive.length () > 0) {
                receive = receive.substring ( 0, receive.length () - 1 );
            }

            //寄件人
            String re = maintain.getSend();
            String[] splits = re.split ( "," );
            String receives = "";
            if (split != null && splits.length > 0) {
                for (String s : splits) {
                    String names = usersMapper.getUsernameByUserId ( s );
                        if (names != null && names != "") {
                            receives = receives + names + ",";
                    }

                }
            }
            if (receives.length () > 0) {
                receives = receives.substring ( 0, receives.length () - 1 );
            }
            maintain.setUsers ( receive);//当前使用者id
            maintain.setSender (receives );//寄件人id

            json.setObject ( maintain );
            json.setMsg ( "ok" );
            json.setFlag ( 0 );
        } catch (Exception e) {
            json.setFlag ( 1 );
            json.setMsg ( e.getMessage () );
            e.printStackTrace ();
        }
        return json;

    }

    public ToJson<Object> deleteByPrimaryKey(Integer id) {
        ToJson<Object> toJson = new ToJson<> ();
        try {
            Integer delete = maintainMapper.deleteByPrimaryKey ( id );
            if (delete > 0) {
                toJson.setMsg ( "删除成功" );
                toJson.setFlag ( 0 );
            }
        } catch (Exception e) {
            e.getMessage ();
            toJson.setMsg ( "删除失败" );
            toJson.setFlag ( 1 );
        }
        return toJson;
    }

    public ToJson<Object> updateByPrimaryKeySelective(Maintain record) {
        ToJson<Object> json = new ToJson<Object> ( 1, "err" );
        try {
            int count = maintainMapper.updateByPrimaryKeySelective ( record );
            if (count > 0) {
                json.setMsg ( "ok" );
                json.setFlag ( 0 );
            }

        } catch (Exception e) {
            json.setFlag ( 1 );
            json.setMsg ( e.getMessage () );
            e.printStackTrace ();
        }
        return json;
    }

    public ToJson<Maintain> selectByExample(Maintain record,String uid) {
        ToJson<Maintain> json = new ToJson<Maintain> ();
        List<Maintain> result = new ArrayList<Maintain> ();
        Map<String, Object> map = new HashMap<String, Object> ();
        map.put ( "Maintain", record.getUid() );
        List<Maintain> maintain = maintainMapper.selectByExample ( map );
        try {
            if (maintain != null) {
                for (Maintain m : maintain) {
                    Maintain ma = new Maintain ();
                    if (uid.equals ( m.getUid () )) {
                        //当前使用者
                        String records = m.getUser ();
                        String[] split = records.split ( "," );
                        String receive = "";
                        if (split != null && split.length > 0) {
                            for (String s : split) {
                                String name = usersMapper.getUsernameByUserId ( s );
                                if (name != null && name != "") {
                                    receive = receive + name + ",";
                                }

                            }
                        }
                        if (receive.length () > 0) {
                            receive = receive.substring ( 0, receive.length () - 1 );
                        }

                        //寄件人
                        String re = m.getSend();
                        String[] splits = re.split ( "," );
                        String receives = "";
                        if (split != null && splits.length > 0) {
                            for (String s : splits) {
                                String names = usersMapper.getUsernameByUserId ( s );
                                if (names != null && names != "") {
                                    receives = receives + names + ",";
                                }

                            }
                        }
                        if (receives.length () > 0) {
                            receives = receives.substring ( 0, receives.length () - 1 );
                        }
                        ma.setId ( m.getId () );
                        ma.setCreateTime ( m.getCreateTime () );//时间
                        ma.setUser ( receive );//当前使用者
                        ma.setUsers ( m.getUser() );//当前使用者id
                        ma.setDescription( m.getDescription() );//问题描述
                        ma.setBusiness( m.getBusiness() );//维修商家
                        ma.setContact ( m.getContact() ); //商家联系人
                        ma.setPhone ( m.getPhone() );//商家电话
                        ma.setAddress( m.getAddress() );//商家地址
                        ma.setSend ( receives );//寄件人
                        ma.setSender ( m.getSend() );//寄件人id
                        result.add ( ma );
                    }
                }
            }
            if (result != null) {
                json.setObj ( result );
                json.setMsg ( "ok" );
                json.setFlag ( 0 );
            }
        } catch (Exception e) {
            json.setFlag ( 1 );
            json.setMsg ( e.getMessage () );
            e.printStackTrace ();
        }
        return json;
    }


}
